/**
  ******************************************************************************
  * @file    app_start.c
  * @author  Feirui_Joe
  * @version V0.1
  * @date    07/26/2010
  * @brief   Initial all Periphs
  ******************************************************************************
  * @copy
  *
  * <h2><center>&copy; COPYRIGHT 2010 Feirui</center></h2>
  */ 


/**
  * @brief INCLUDE FILES.
  */
#ifndef	APP_GLOBALS_START
#define	APP_GLOBALS_START
#include <includes.h>
#include "debug.h"

#include "SRAM_Driver.h"
#include "EEPROM_Driver.h"
#include "app_Memory.h"
#include "PrimeMotor_Driver.h"
#include "TempControl_Driver.h"

/**
  * @brief Functions.
  */
static void Start_EventCreate(void);
static void Start_TaskCreate(void);
static void Start_OSTimerCreate(void);

#ifdef ENABLE_WATCHDOG_OPT
static void Start_ConfigWatchdog (void);
#endif


static void Start_ResetTaskCommMedium(void);
static void Start_GenericInit(void);
static void app_Task_CommonHandle(void);

/**
  * @brief  The startup task.  The uC/OS-II ticker should only be initialize once multitasking starts.
  * @param  p_arg       Argument passed to 'App_TaskStart()' by 'OSTaskCreate()'
  * @return none.
  * @Caller This is a task.
  * @Note   StartupTask also is KeypadTask.
  */
void App_Task_START(void *p_arg)
{
  uint8_t os_err;
  
	(void)p_arg;
	
  /* Setup SysTick Timer "OS_TICKS_PER_SEC" Tick per second  */
	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  if (SysTick_Config(SystemCoreClock / OS_TICKS_PER_SEC))
    /* Capture error */ 
    NVIC_SystemReset();										                    /* Resets the System		  */
  
#if (OS_TASK_STAT_EN > 0)
  OSStatInit();                                               /* Determine CPU capacity. */
#endif
  
  /***********************************************/
  /* HGM63 Module Start  */
  /***********************************************/   
  
  /* Initial Status RAM       */

  /* Initial System interface */
  
  /* Create application events.  */
  Start_EventCreate();

	/* Create OS Timer*/
	Start_OSTimerCreate();

  /* Reset All Buffer data */
  Start_ResetTaskCommMedium();

  /* Create application tasks.   */
  Start_TaskCreate();

  /**************************/

  
  /* Enable Interface with Computer   */
  
  /* Wait for Start action finish */
  // OSSemPend(App_Sem_STARTSYNC,0,&os_err);
  
  /* Start Software Timer */
  OSTmrStart(App_Tmr_TIMER01HZ,&os_err);
  OSTmrStart(App_Tmr_TIMER05HZ,&os_err);
  OSTmrStart(App_Tmr_TIMER10HZ,&os_err);
  OSTmrStart(App_Tmr_TIMER20HZ,&os_err);
  OSTmrStart(App_Tmr_TIMER40HZ,&os_err);

  // Debug_init();

  /*  set nvic interrupt priority group */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  
  /* generic Initialization */
  Start_GenericInit();
  
  /* Enable Watchdog */
#ifdef ENABLE_WATCHDOG_OPT
  Start_ConfigWatchdog();
#endif
  /* common task */
  app_Task_CommonHandle();

  /* Normally it wouldn't get here */
  OSTaskSuspend(APP_TASK_PRIO_START);   //将初始任务悬挂起来
}



#ifdef ENABLE_WATCHDOG_OPT
/**
  * @brief  Config Watchdog
  * @param  None
  * @retval None
  * @Note   setting for Watchdog
  */
static void Start_ConfigWatchdog(void)
{

  /* IWDG timeout equal to 5 s (the timeout may varies due to LSI frequency
     dispersion) */
  /* Enable write access to IWDG_PR and IWDG_RLR registers */
  IWDG_WriteConfig(IWDG_WRITE_ENABLE);

  /* IWDG counter clock: 40KHz(LSI) / 64 = 625 Hz */
  IWDG_SetPrescalerDiv(IWDG_PRESCALER_DIV64);

  /* Set counter reload value to 625*5 */
  IWDG_CntReload(625*5);

  /* Reload IWDG counter */
  IWDG_ReloadKey();

  /* Enable IWDG (the LSI oscillator will be enabled by hardware) */
  IWDG_Enable();
}
#endif




                                       /************************************/
                                       /*   Initial OS function            */
                                       /************************************/

/**
  * @brief  Create the application events.
  * @param  none.
  * @return none.
  * @Caller App_TaskStart().
  * @Note   none.
  */
static void Start_EventCreate(void)
{
  uint8_t  os_err;
  
  /***************************************************************************************/
  /* Sem */
  /* Create Sem for Start Sync. 目的是为了让某个任务优先运行一些关键操作，然后释放信号量让其它任务继续运行 */
  App_Sem_STARTSYNC = OSSemCreate(0);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_STARTSYNC, (uint8_t *)App_SEM_NAME_STARTSYNC, &os_err);
#endif

  /* Create Sem for WORKFLOWCMD.           */
  App_Sem_WORKFLOWCMD = OSSemCreate(1);

  /* Create Sem for WIFI TX.           */
  App_Sem_NETWORKTX = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_NETWORKTX, (uint8_t *)App_SEM_NAME_NETWORKTX, &os_err);
#endif

/* Create Sem for SRAM Transfer Complete.           */
  App_Sem_SRAMRC = OSSemCreate(0);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_SRAMRC, (uint8_t *)App_SEM_NAME_SRAMRC, &os_err);
#endif

/* Create Sem for EEPROM Transfer Complete.           */
  App_Sem_EEPROMRC = OSSemCreate(0);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_EEPROMRC, (uint8_t *)App_SEM_NAME_EEPROMRC, &os_err);
#endif

/* Create Sem for RS485 Transfer Complete.           */
  App_Sem_RS485TC = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_RS485TC, (uint8_t *)App_SEM_NAME_RS485TC, &os_err);
#endif

/* Create Sem for RS232 Transfer Complete.           */
  App_Sem_RS232TC = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_RS232TC, (uint8_t *)App_SEM_NAME_RS232TC, &os_err);
#endif

/* Create Sem for RS485 Bus Idle.           */
  App_Sem_RS485BUSIDLE = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_RS485BUSIDLE, (uint8_t *)App_SEM_NAME_RS485BUSIDLE, &os_err);
#endif

/* Create Sem for Pmotor Recv complete.           */
  App_Sem_PMOTORRC = OSSemCreate(0);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_PMOTORRC, (uint8_t *)App_SEM_NAME_PMOTORRC, &os_err);
#endif

/* Create Sem for TempControl Recv complete.           */
  App_Sem_TEMPCTLRC = OSSemCreate(0);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_TEMPCTLRC, (uint8_t *)App_SEM_NAME_TEMPCTLRC, &os_err);
#endif

/* Create Sem for rtc.           */
  App_Sem_RTCADJ = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_RTCADJ, (uint8_t *)App_SEM_NAME_RTC, &os_err);
#endif

/* Create Sem for database.           */
  App_Sem_DATABASERW = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_DATABASERW, (uint8_t *)App_SEM_NAME_DATABASERW, &os_err);
#endif

/* Create Sem for network test .           */
  App_Sem_NETWORKTEST = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_NETWORKTEST, (uint8_t *)App_SEM_NAME_NETWORKTEST, &os_err);
#endif

/* Create Sem for memoryee .           */
  App_Sem_MOMERYEE = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_MOMERYEE, (uint8_t *)App_SEM_NAME_MEMOTYEE, &os_err);
#endif

  App_Sem_WORKFLOW = OSSemCreate(1);
  App_Sem_OUTPUTS = OSSemCreate(1);
  App_Sem_OPERATE = OSSemCreate(1);

/* Create Sem for DBPAGE0 .           */
  App_Sem_DBPAGE0 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_DBPAGE0, (uint8_t *)App_SEM_NAME_DBPAGE0, &os_err);
#endif

/* Create Sem for DBPAGE1 .           */
  App_Sem_DBPAGE1 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_DBPAGE1, (uint8_t *)App_SEM_NAME_DBPAGE1, &os_err);
#endif

/* Create Sem for DBPAGE2 .           */
  App_Sem_DBPAGE2 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_DBPAGE2, (uint8_t *)App_SEM_NAME_DBPAGE2, &os_err);
#endif

/* Create Sem for DBPAGE3 .           */
  App_Sem_DBPAGE3 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_DBPAGE3, (uint8_t *)App_SEM_NAME_DBPAGE3, &os_err);
#endif

/* Create Sem for DBPAGE4 .           */
  App_Sem_DBPAGE4 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_DBPAGE4, (uint8_t *)App_SEM_NAME_DBPAGE4, &os_err);
#endif

/* Create Sem for DBPAGE5 .           */
  App_Sem_DBPAGE5 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_DBPAGE5, (uint8_t *)App_SEM_NAME_DBPAGE5, &os_err);
#endif

/* Create Sem for DBPAGE6 .           */
  App_Sem_DBPAGE6 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_DBPAGE6, (uint8_t *)App_SEM_NAME_DBPAGE6, &os_err);
#endif

/* Create Sem for DBPAGE7 .           */
  App_Sem_DBPAGE7 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_DBPAGE7, (uint8_t *)App_SEM_NAME_DBPAGE7, &os_err);
#endif

/* Create Sem for DBPAGE8 .           */
  App_Sem_DBPAGE8 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_DBPAGE8, (uint8_t *)App_SEM_NAME_DBPAGE8, &os_err);
#endif

/* Create Sem for CFGPAGE0 .           */
  App_Sem_CFGPAGE0 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE0, (uint8_t *)App_SEM_NAME_CFGPAGE0, &os_err);
#endif

/* Create Sem for CFGPAGE1 .           */
  App_Sem_CFGPAGE1 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE1, (uint8_t *)App_SEM_NAME_CFGPAGE1, &os_err);
#endif

/* Create Sem for CFGPAGE2 .           */
  App_Sem_CFGPAGE2 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE2, (uint8_t *)App_SEM_NAME_CFGPAGE2, &os_err);
#endif

/* Create Sem for CFGPAGE3 .           */
  App_Sem_CFGPAGE3 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE3, (uint8_t *)App_SEM_NAME_CFGPAGE3, &os_err);
#endif

/* Create Sem for CFGPAGE4 .           */
  App_Sem_CFGPAGE4 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE4, (uint8_t *)App_SEM_NAME_CFGPAGE4, &os_err);
#endif

/* Create Sem for CFGPAGE5 .           */
  App_Sem_CFGPAGE5 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE5, (uint8_t *)App_SEM_NAME_CFGPAGE5, &os_err);
#endif

/* Create Sem for CFGPAGE6 .           */
  App_Sem_CFGPAGE6 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE6, (uint8_t *)App_SEM_NAME_CFGPAGE6, &os_err);
#endif

/* Create Sem for CFGPAGE7 .           */
  App_Sem_CFGPAGE7 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE7, (uint8_t *)App_SEM_NAME_CFGPAGE7, &os_err);
#endif

/* Create Sem for CFGPAGE8 .           */
  App_Sem_CFGPAGE8 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE8, (uint8_t *)App_SEM_NAME_CFGPAGE8, &os_err);
#endif

/* Create Sem for CFGPAGE9 .           */
  App_Sem_CFGPAGE9 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE9, (uint8_t *)App_SEM_NAME_CFGPAGE9, &os_err);
#endif

/* Create Sem for CFGPAGE10 .           */
  App_Sem_CFGPAGE10 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE10, (uint8_t *)App_SEM_NAME_CFGPAGE10, &os_err);
#endif

/* Create Sem for CFGPAGE11 .           */
  App_Sem_CFGPAGE11 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE11, (uint8_t *)App_SEM_NAME_CFGPAGE11, &os_err);
#endif

/* Create Sem for CFGPAGE12 .           */
  App_Sem_CFGPAGE12 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE12, (uint8_t *)App_SEM_NAME_CFGPAGE12, &os_err);
#endif

/* Create Sem for CFGPAGE13 .           */
  App_Sem_CFGPAGE13 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE13, (uint8_t *)App_SEM_NAME_CFGPAGE13, &os_err);
#endif

/* Create Sem for CFGPAGE14 .           */
  App_Sem_CFGPAGE14 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE14, (uint8_t *)App_SEM_NAME_CFGPAGE14, &os_err);
#endif

/* Create Sem for CFGPAGE15 .           */
  App_Sem_CFGPAGE15 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE15, (uint8_t *)App_SEM_NAME_CFGPAGE15, &os_err);
#endif

/* Create Sem for CFGPAGE16 .           */
  App_Sem_CFGPAGE16 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE16, (uint8_t *)App_SEM_NAME_CFGPAGE16, &os_err);
#endif

/* Create Sem for CFGPAGE17 .           */
  App_Sem_CFGPAGE17 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_CFGPAGE17, (uint8_t *)App_SEM_NAME_CFGPAGE17, &os_err);
#endif

/* Create Sem for TMPPAGE0 .           */
  App_Sem_TMPPAGE0 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_TMPPAGE0, (uint8_t *)App_SEM_NAME_TMPPAGE0, &os_err);
#endif

/* Create Sem for TMPPAGE1 .           */
  App_Sem_TMPPAGE1 = OSSemCreate(1);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Sem_TMPPAGE1, (uint8_t *)App_SEM_NAME_TMPPAGE1, &os_err);
#endif

  /****************************************************************************************/
  /* Flag */
  /* Create Flag GRP for ACAD Task */
  App_Flags_ACAD = OSFlagCreate(OSACADFlags_DefaultValue, &os_err);
#if (OS_FLAG_NAME_EN > 0u)
  OSFlagNameSet(App_Flags_ACAD, (uint8_t *)App_FLAGS_NAME_ACAD, &os_err);
#endif

  /* Create Flag GRP for WIFI Task */
  App_Flags_NETWORK = OSFlagCreate(0,&os_err);
#if (OS_FLAG_NAME_EN > 0u)
  OSFlagNameSet(App_Flags_NETWORK, (uint8_t *)App_FLAGS_NAME_WIFI, &os_err);
#endif

  App_Flags_WORKFLOW = OSFlagCreate(0,&os_err);

  /* Create Flag GRP for WIFI HandleCache Task */
  App_Flags_NETPARSE = OSFlagCreate(0,&os_err);
#if (OS_FLAG_NAME_EN > 0u)
  OSFlagNameSet(App_Flags_NETPARSE, (uint8_t *)App_FLAGS_NAME_NETPARSE, &os_err);
#endif

  /* Create Flag GRP for PID Task */
  App_Flags_PID = OSFlagCreate(OSPIDFlags_DefaultValue, &os_err);
#if (OS_FLAG_NAME_EN > 0u)
  OSFlagNameSet(App_Flags_PID, (uint8_t *)App_FLAGS_NAME_PID, &os_err);
#endif

  /* Create Flag GRP for Common Task */
  App_Flags_COMMON = OSFlagCreate(0, &os_err);
#if (OS_FLAG_NAME_EN > 0u)
  OSFlagNameSet(App_Flags_COMMON, (uint8_t *)App_FLAGS_NAME_COMMON, &os_err);
#endif

  /* Create Flag GRP for Common Task */
  App_Flags_RS485Handle = OSFlagCreate(0, &os_err);
#if (OS_FLAG_NAME_EN > 0u)
  OSFlagNameSet(App_Flags_RS485Handle, (uint8_t *)App_FLAGS_NAME_RS485Handle, &os_err);
#endif

  /* Create Flag GRP for COMMUNI Task */
  App_Flags_COMMUNI = OSFlagCreate(OSCOMMUNIFlags_DefaultValue,&os_err);
#if (OS_FLAG_NAME_EN > 0u)
  OSFlagNameSet(App_Flags_COMMUNI, (uint8_t *)App_FLAGS_NAME_COMMUNI, &os_err);
#endif

  /* Create Flag GRP for COMMUNI Task */
  App_Flags_Alarm = OSFlagCreate(OSALARMFlags_DefaultValue,&os_err);
#if (OS_FLAG_NAME_EN > 0u)
  OSFlagNameSet(App_Flags_Alarm, (uint8_t *)App_FLAGS_NAME_ALARM, &os_err);
#endif

    /* Create Flag GRP for RELAY Task */
  App_Flags_RELAY = OSFlagCreate(0,&os_err);
  
  App_Flags_DSTIN = OSFlagCreate(0,&os_err);

  /********************************************************************************/
  /* Queue */
  /* Create Q for ACAD Task */
  App_Q_ACAD = OSQCreate((void **)&ACRecdataMsg[0], ACADRecdataQNum);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Q_ACAD, (uint8_t *)App_Q_NAME_ACAD, &os_err);
#endif

  /* Create Q for HandleSerData Task */
  App_Q_NETHANDLE = OSQCreate(&DataHandleMsg[0],NetDataHandleQNum);
#if (OS_EVENT_NAME_EN > 0u)
  OSEventNameSet(App_Q_NETHANDLE, (uint8_t *)App_Q_NAME_NETHANDLE, &os_err);
#endif

}


static void Start_OSTimerCreate(void)
{
	uint8_t os_err;
  /* Create Tmr for 01HZ */
  App_Tmr_TIMER01HZ=OSTmrCreate((uint32_t       )OS_TMR_CFG_TICKS_PER_SEC,
                                (uint32_t       )OS_TMR_CFG_TICKS_PER_SEC,
                                (uint8_t        )OS_TMR_OPT_PERIODIC,
                                (OS_TMR_CALLBACK)App_Tmr_TIMER01HZCallback,
                                (void          *)0,
                                (uint8_t       *)App_TMR_NAME_TIMER01HZ,
                                &os_err);

  /* Create Tmr for 5HZ */
  App_Tmr_TIMER05HZ=OSTmrCreate((uint32_t       )OS_TMR_CFG_TICKS_PER_SEC/5,
                                (uint32_t       )OS_TMR_CFG_TICKS_PER_SEC/5,
                                (uint8_t        )OS_TMR_OPT_PERIODIC,
                                (OS_TMR_CALLBACK)App_Tmr_TIMER05HZCallback,
                                (void          *)0,
                                (uint8_t       *)App_TMR_NAME_TIMER05HZ,
                                &os_err);

    /* Create Tmr for 10HZ */
  App_Tmr_TIMER10HZ=OSTmrCreate   ((uint32_t       )OS_TMR_CFG_TICKS_PER_SEC/10,
                                   (uint32_t       )OS_TMR_CFG_TICKS_PER_SEC/10,
                                   (uint8_t        )OS_TMR_OPT_PERIODIC,
                                   (OS_TMR_CALLBACK)App_Tmr_TIMER10HZCallback,
                                   (void          *)0,
                                   (uint8_t       *)App_TMR_NAME_TIMER10HZ,
                                   &os_err);

    /* Create Tmr for 20HZ */
  App_Tmr_TIMER20HZ=OSTmrCreate   ((uint32_t       )OS_TMR_CFG_TICKS_PER_SEC/20,
                                   (uint32_t       )OS_TMR_CFG_TICKS_PER_SEC/20,
                                   (uint8_t        )OS_TMR_OPT_PERIODIC,
                                   (OS_TMR_CALLBACK)App_Tmr_TIMER20HZCallback,
                                   (void          *)0,
                                   (uint8_t       *)App_TMR_NAME_TIMER20HZ,
                                   &os_err);
  /* Create Tmr for 40HZ */
  App_Tmr_TIMER40HZ=OSTmrCreate((uint32_t       )OS_TMR_CFG_TICKS_PER_SEC/40,
                                (uint32_t       )OS_TMR_CFG_TICKS_PER_SEC/40,
                                (uint8_t        )OS_TMR_OPT_PERIODIC,
                                (OS_TMR_CALLBACK)App_Tmr_TIMER40HZCallback,
                                (void          *)0,
                                (uint8_t       *)App_TMR_NAME_TIMER100HZ,
                                &os_err);
}




/**
  * @brief  Initial Data RAM buffer
  * @param  none
  * @return none.
  */
static void Start_ResetTaskCommMedium(void)
{
 
}

/// @brief 通用初始化函数
/// @param  none
static void Start_GenericInit(void){
    // 实时时钟
    rtc_Initial();
    // 扩展缓存
    SRAM_Init();
    // eeprom
    EEPROM_Init();
    // 共享缓冲
    sharebuffer_Init();
    // 数据库初始化
    DataBase_Init();
}

static void BeforeRealTimeData(void)
{
    uint16_t TemLIMITAlarm=LIMITAlarm;
    uint64_t TemControlAlarm=ControlAlarm;
    ExchangeShareBufferDataContent(NULL,CfgOperaType_None,(uint8_t*)&TemLIMITAlarm,&FlagShareBuffer_RawAlarm[RawAlarm_DI_BVOL_LLimit/8],2);
    ExchangeShareBufferDataContent(NULL,CfgOperaType_None,(uint8_t*)&TemControlAlarm,&FlagShareBuffer_RawAlarm[RawAlarm_BVOL_Mac_Fail/8],5);
    ControlAlarm = TemControlAlarm;
    LIMITAlarm = TemLIMITAlarm;
}

/// @brief 公共处理函数
/// @param  none
void app_Task_CommonHandle(void){
    uint8_t os_err;
    uint8_t ramUpFailCnt = 0, eeUpFailCnt = 0;
    uint16_t ramUpdateFlag = 0, eepromUpdateFlag = DEF_TRUE;
    OS_FLAGS os_value;
    static uint8_t cnt = 0;

    while (DEF_TRUE){
        /*  Wait for State Update output Flags  */
        os_value = OSFlagPend(App_Flags_COMMON,
                              OSCommFlags_ALL,
                              OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME,
                              0,
                              &os_err);
        if (os_err != OS_ERR_NONE)
            SYS_DebugERROR;
        
        // 1秒钟定时器
        if(os_value & OSCommFlags_Timing){
            // 首次运行先从eeprom中读取数据到RAM,失败则重试
            if(ramUpdateFlag == 0 && ramUpFailCnt <= 5){
                ramUpdateFlag = Memory_UpdateCfgEEpromToRam();
                if(ramUpdateFlag == 0)
                    ramUpFailCnt++;
                else
                    ramUpFailCnt = 0;
            }
            // 如果写eeprom数据失败则进行重试
            if(eepromUpdateFlag == DEF_FALSE && eeUpFailCnt <= 5){
                // 发送EEPROM写入事件
                OSFlagPost(App_Flags_COMMON,OSCommFlags_UpdateCfg,OS_FLAG_SET,&os_err);
                eeUpFailCnt++;
            }
        }
        // 实时数据定时更新 1秒定时器
        if(os_value & OSCommFlags_Timing){
            BeforeRealTimeData();
            sharebuff_RealTimeDataUpdate();
            sharebuff_RealTimeDataUpdate_CKP();

            // if(cnt==0)
            // {
            //   sharebuff_RealTimeDataUpdate_CKP();
            //   cnt=1;
            // }
        }
        // 如果有配置参数写入则更新EEPROM
        if(os_value & OSCommFlags_UpdateCfg){
            eepromUpdateFlag = Memory_UpdateCfgRamToEEprom();
            if(eepromUpdateFlag == DEF_TRUE)
                eeUpFailCnt = 0;
        }
    }
}

/// @brief RS485发送处理函数
/// @param  none
void App_Task_RS485SendHandle(void){
    uint8_t i = 0;
    uint8_t os_err;
    OS_FLAGS os_value;

    while (DEF_TRUE){
        /*  Wait for State Update output Flags  */
        os_value = OSFlagPend(App_Flags_RS485Handle,
                              RS485HandleFlags_ALL,
                              OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME,
                              0,
                              &os_err);
        if (os_err != OS_ERR_NONE)
            SYS_DebugERROR;
        
        // 如果是操作原动机
        if(os_value & RS485HandleFlags_PMotorOperate){
            for(i = 0; i < 5; i++)
              if(PMotor_FuncOperate() != DEF_FALSE)
                break;
        }
        // 如果是操作温控器
        if(os_value & RS485HandleFlags_TempCtlOperate){
            for(i = 0; i < 5; i++)
              if(TempCtl_FuncOperate() != DEF_FALSE)
                break;
        }
        // 每秒获取一次控制器数据
        if(os_value & RS485HandleFlags_Timing){
            PMotor_QueryStatus();
            TempCtl_QueryStatus();
        }
    }
}

/**
  * @brief  Create the application tasks.
  * @param  none.
  * @return none.
  * @Caller App_TaskStart().
  * @Note   none.
  */
static void Start_TaskCreate(void)
{
#ifdef EnableCheckOS_errOpt
    uint8_t  os_err;
#endif
/*             Create ACAD Task                   */
#ifdef EnableCheckOS_errOpt
    os_err = 
#endif
    OSTaskCreateExt((void (*)(void *)) App_Task_ACAD,
                    (void          * ) 0,
                    (OS_STK        * )&App_TaskStk_ACAD[APP_TASK_STK_SIZE_ACAD - 1],
                    (INT8U           ) APP_TASK_PRIO_ACAD,
                    (INT16U          ) APP_TASK_PRIO_ACAD,
                    (OS_STK        * )&App_TaskStk_ACAD[0],
                    (INT32U          ) APP_TASK_STK_SIZE_ACAD,
                    (void          * ) 0,
                    (INT16U          )(OS_TASK_OPT_STK_CLR | OS_TASK_OPT_STK_CHK));
#if (OS_TASK_NAME_EN > 0u)
    OSTaskNameSet(APP_TASK_PRIO_ACAD, (uint8_t *)APP_TASK_NAME_ACAD, &os_err);
#endif

/*             Create PID Task                   */
#ifdef EnableCheckOS_errOpt
    os_err = 
#endif
    OSTaskCreateExt((void (*)(void *)) App_Task_PID,
                    (void          * ) 0,
                    (OS_STK        * )&App_TaskStk_PID[APP_TASK_STK_SIZE_PID - 1],
                    (INT8U           ) APP_TASK_PRIO_PID,
                    (INT16U          ) APP_TASK_PRIO_PID,
                    (OS_STK        * )&App_TaskStk_PID[0],
                    (INT32U          ) APP_TASK_STK_SIZE_PID,
                    (void          * ) 0,
                    (INT16U          )(OS_TASK_OPT_STK_CLR | OS_TASK_OPT_STK_CHK));
#if (OS_TASK_NAME_EN > 0u)
    OSTaskNameSet(APP_TASK_PRIO_PID, (uint8_t *)APP_TASK_NAME_PID, &os_err);
#endif

    /*             Create Relay Output Task              */
#ifdef EnableCheckOS_errOpt
    os_err = 
#endif
    OSTaskCreateExt         ((void (*)(void *)) App_Task_RELAY,
                             (void          * ) 0,
                             (OS_STK        * )&App_TaskStk_RELAY   [APP_TASK_STK_SIZE_RELAY - 1],
                             (INT8U           ) APP_TASK_PRIO_RELAY,
                             (INT16U          ) APP_TASK_PRIO_RELAY,
                             (OS_STK        * )&App_TaskStk_RELAY   [0],
                             (INT32U          ) APP_TASK_STK_SIZE_RELAY,
                             (void          * ) 0,
                             (INT16U          )(OS_TASK_OPT_STK_CLR | OS_TASK_OPT_STK_CHK));
#if (OS_TASK_NAME_EN > 0u)
    OSTaskNameSet(APP_TASK_PRIO_RELAY, (uint8_t *)APP_TASK_NAME_RELAY, &os_err);
#endif

#ifdef EnableCheckOS_errOpt
    os_err = 
#endif
    OSTaskCreateExt         ((void (*)(void *)) App_Task_DSTIN,
                             (void          * ) 0,
                             (OS_STK        * )&App_TaskStk_DSTIN   [APP_TASK_STK_SIZE_DSTIN - 1],
                             (INT8U           ) APP_TASK_PRIO_DSTIN,
                             (INT16U          ) APP_TASK_PRIO_DSTIN,
                             (OS_STK        * )&App_TaskStk_DSTIN   [0],
                             (INT32U          ) APP_TASK_STK_SIZE_DSTIN,
                             (void          * ) 0,
                             (INT16U          )(OS_TASK_OPT_STK_CLR | OS_TASK_OPT_STK_CHK));
#if (OS_TASK_NAME_EN > 0u)
    OSTaskNameSet(APP_TASK_PRIO_DSTIN, (uint8_t *)APP_TASK_NAME_DSTIN, &os_err);
#endif

#ifdef EnableCheckOS_errOpt    
    os_err = 
#endif
    OSTaskCreateExt((void (*)(void *)) App_Task_WORKFLOW,
                    (void          * ) 0,
                    (OS_STK        * )&App_TaskStk_WORKFLOW[APP_TASK_STK_SIZE_WORKFLOW - 1],
                    (INT8U           ) APP_TASK_PRIO_WORKFLOW,
                    (INT16U          ) APP_TASK_PRIO_WORKFLOW,
                    (OS_STK        * )&App_TaskStk_WORKFLOW[0],
                    (INT32U          ) APP_TASK_STK_SIZE_WORKFLOW,
                    (void          * ) 0,
                    (INT16U          )(OS_TASK_OPT_STK_CLR | OS_TASK_OPT_STK_CHK));



/*             NetWork flow Task                   */
#ifdef EnableCheckOS_errOpt
    os_err = 
#endif
    OSTaskCreateExt((void (*)(void *)) App_Task_NetWork_Flow,
                    (void          * ) 0,
                    (OS_STK        * )&App_TaskStk_NETFLOW[APP_TASK_STK_SIZE_NETFLOW - 1],
                    (INT8U           ) APP_TASK_PRIO_NETFLOW,
                    (INT16U          ) APP_TASK_PRIO_NETFLOW,
                    (OS_STK        * )&App_TaskStk_NETFLOW[0],
                    (INT32U          ) APP_TASK_STK_SIZE_NETFLOW,
                    (void          * ) 0,
                    (INT16U          )(OS_TASK_OPT_STK_CLR | OS_TASK_OPT_STK_CHK));
#if (OS_TASK_NAME_EN > 0u)
    OSTaskNameSet(APP_TASK_PRIO_NETFLOW, (uint8_t *)APP_TASK_NAME_NETFLOW, &os_err);
#endif

/*             NetWork Data Parse Task                   */
#ifdef EnableCheckOS_errOpt
    os_err = 
#endif
    OSTaskCreateExt((void (*)(void *)) App_Task_NetWork_Parse,
                    (void          * ) 0,
                    (OS_STK        * )&App_TaskStk_NETPARSE[APP_TASK_STK_SIZE_NETPARSE - 1],
                    (INT8U           ) APP_TASK_PRIO_NETPARSE,
                    (INT16U          ) APP_TASK_PRIO_NETPARSE,
                    (OS_STK        * )&App_TaskStk_NETPARSE[0],
                    (INT32U          ) APP_TASK_STK_SIZE_NETPARSE,
                    (void          * ) 0,
                    (INT16U          )(OS_TASK_OPT_STK_CLR | OS_TASK_OPT_STK_CHK));
#if (OS_TASK_NAME_EN > 0u)
    OSTaskNameSet(APP_TASK_PRIO_NETPARSE, (uint8_t *)APP_TASK_NAME_NETPARSE, &os_err);
#endif

/*             NetWork Data Handle Task                   */
#ifdef EnableCheckOS_errOpt
    os_err = 
#endif
    OSTaskCreateExt((void (*)(void *)) App_Task_NetWork_Handle,
                    (void          * ) 0,
                    (OS_STK        * )&App_TaskStk_NETHANDLE[APP_TASK_STK_SIZE_NETHANDLE - 1],
                    (INT8U           ) APP_TASK_PRIO_NETHANDLE,
                    (INT16U          ) APP_TASK_PRIO_NETHANDLE,
                    (OS_STK        * )&App_TaskStk_NETHANDLE[0],
                    (INT32U          ) APP_TASK_STK_SIZE_NETHANDLE,
                    (void          * ) 0,
                    (INT16U          )(OS_TASK_OPT_STK_CLR | OS_TASK_OPT_STK_CHK));
#if (OS_TASK_NAME_EN > 0u)
    OSTaskNameSet(APP_TASK_PRIO_NETHANDLE, (uint8_t *)APP_TASK_NAME_NETHANDLE, &os_err);
#endif

/*             Create COMM Task                   */
#ifdef EnableCheckOS_errOpt
  os_err = 
#endif
    OSTaskCreateExt         ((void (*)(void *)) App_Task_COMMUNI,
                             (void          * ) 0,
                             (OS_STK        * )&App_TaskStk_COMMUNI[APP_TASK_STK_SIZE_COMMUNI - 1],
                             (INT8U           ) APP_TASK_PRIO_COMMUNI,
                             (INT16U          ) APP_TASK_PRIO_COMMUNI,
                             (OS_STK        * )&App_TaskStk_COMMUNI[0],
                             (INT32U          ) APP_TASK_STK_SIZE_COMMUNI,
                             (void          * ) 0,
                             (INT16U          )(OS_TASK_OPT_STK_CLR | OS_TASK_OPT_STK_CHK));
#if (OS_TASK_NAME_EN > 0u)
    OSTaskNameSet(APP_TASK_PRIO_COMMUNI, (uint8_t *)APP_TASK_NAME_COMMUNI, &os_err);
#endif

/*             Create COMM Task                   */
#ifdef EnableCheckOS_errOpt
  os_err = 
#endif
    OSTaskCreateExt         ((void (*)(void *)) App_Task_Alarm,
                             (void          * ) 0,
                             (OS_STK        * )&App_TaskStk_Alarm[APP_TASK_STK_SIZE_ALARM - 1],
                             (INT8U           ) APP_TASK_PRIO_ALARM,
                             (INT16U          ) APP_TASK_PRIO_ALARM,
                             (OS_STK        * )&App_TaskStk_Alarm[0],
                             (INT32U          ) APP_TASK_STK_SIZE_ALARM,
                             (void          * ) 0,
                             (INT16U          )(OS_TASK_OPT_STK_CLR | OS_TASK_OPT_STK_CHK));
#if (OS_TASK_NAME_EN > 0u)
    OSTaskNameSet(APP_TASK_PRIO_ALARM, (uint8_t *)APP_TASK_NAME_ALARM, &os_err);
#endif

/*             Create RS485 COMM Task                   */
#ifdef EnableCheckOS_errOpt
  os_err = 
#endif
    OSTaskCreateExt         ((void (*)(void *)) App_Task_RS485SendHandle,
                             (void          * ) 0,
                             (OS_STK        * )&App_TaskStk_RS485Handle[APP_TASK_STK_SIZE_RS485HANDLE - 1],
                             (INT8U           ) APP_TASK_PRIO_RS485Handle,
                             (INT16U          ) APP_TASK_PRIO_RS485Handle,
                             (OS_STK        * )&App_TaskStk_RS485Handle[0],
                             (INT32U          ) APP_TASK_STK_SIZE_RS485HANDLE,
                             (void          * ) 0,
                             (INT16U          )(OS_TASK_OPT_STK_CLR | OS_TASK_OPT_STK_CHK));
#if (OS_TASK_NAME_EN > 0u)
    OSTaskNameSet(APP_TASK_PRIO_ALARM, (uint8_t *)APP_TASK_NAME_ALARM, &os_err);
#endif
}



/*$PAGE*/#endif	/* APP_GLOBALS_START */
